package com.awspaas.user.apps.shhtaerospaceindustrial.visiting;

import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListenerInterface;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.shhtaerospaceindustrial.sms.SmsUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

public class IsHospitalOfficeSendAfterLeave extends ExecuteListener implements ExecuteListenerInterface {
	@Override
	public String getDescription() {
		return "是院部职工申请，院办公室审批结束，给接待人发消息！";
	}

	@SuppressWarnings("null")
	@Override
	public void execute(ProcessExecutionContext pec) throws Exception {
		boolean flag_ok = SDK.getTaskAPI().isChoiceActionMenu(pec.getTaskInstance(), "同意");
		ProcessInstance processInstance = pec.getProcessInstance();
		String bindId = processInstance.getId();// 流程实例ID
		String headSql = "select * from BO_EU_INNERLOCATION_VISIT_APP where bindid='" + bindId + "' ";
		List<Map<String, Object>> headList = DBSql.query(headSql, new ColumnMapRowMapper(), new Object[] {});
		if (headList == null || headList.isEmpty()) {
			return;
		}
		Map<String, Object> dataMap = headList.get(0);
		String targetunit = CoreUtil.objToStr(dataMap.get("TARGETUNIT"));// 申请单位
		String targetdept = CoreUtil.objToStr(dataMap.get("TARGETDEPT"));// 申请部门
		String targetman = CoreUtil.objToStr(dataMap.get("TARGETMAN"));// 申请人
		String visitornum = CoreUtil.objToStr(dataMap.get("VISITORNUM"));// 参观总人数
		String targetmanphone = CoreUtil.objToStr(dataMap.get("TARGETMANPHONE"));// 联系电话
		String ptryzzandname = CoreUtil.objToStr(dataMap.get("PTRYZZANDNAME"));// 陪同人员最高职务及姓名
		if (flag_ok && "院部".equals(targetunit)) {
			String bodySql = "select * from BO_EU_INNLOC_VISIT_APP_LOCS where bindid='" + bindId + "' ";
			List<Map<String, Object>> bodyList = DBSql.query(bodySql, new ColumnMapRowMapper(), new Object[] {});
			System.out.println("locs size" + bodyList.size());
			String bodySql1 = "select * from BO_EU_INNLOC_VISIT_APP_CARS where bindid='" + bindId + "' ";
			List<Map<String, Object>> bodyList1 = DBSql.query(bodySql1, new ColumnMapRowMapper(), new Object[] {});
			System.out.println("cars size" + bodyList1.size());
			StringBuffer num = new StringBuffer("");// 总的车牌信息
			StringBuffer inout = new StringBuffer("");// 总的进入园区车辆信息和园区门
			// 含车辆信息参观
			if (bodyList1 != null || bodyList1.size() > 0) {
				for (int j = 0; j < bodyList1.size(); j++) {
					Map<String, Object> bodydata1 = bodyList1.get(j);
					String CARNO = CoreUtil.objToStr(bodydata1.get("CARNO"));// 车牌号
					String INOUTDOOR = CoreUtil.objToStr(bodydata1.get("INOUTDOOR"));// 进出园区门号
					if (j < bodyList1.size() - 1) {
						num.append(CARNO);
						num.append("、");
					} else {
						num.append(CARNO);
						num.append("");
					}
					if (INOUTDOOR != null || !"".equals(INOUTDOOR)) {
						inout.append(CARNO + "（" + INOUTDOOR);
						inout.append("）、");
					}
				}
			}
			System.out.println("num is" + num.toString());
			if (!"".equals(num.toString()) && (bodyList != null || bodyList.size() > 0)) {
				for (int i = 0; i < bodyList.size(); i++) {
					Map<String, Object> bodydata = bodyList.get(i);
					String TARGETUNIT = CoreUtil.objToStr(bodydata.get("TARGETUNIT"));// 参观单位
					String LOCATIONNAME = CoreUtil.objToStr(bodydata.get("LOCATIONNAME"));// 地点名称
					String VISITDATE = CoreUtil.objToStr(bodydata.get("VISITDATE"));// 预计参观日期
					String VISITDATETIME = CoreUtil.objToStr(bodydata.get("VISITDATETIME"));// 参观开始时间
					String VISITDATETIMEEND = CoreUtil.objToStr(bodydata.get("VISITDATETIMEEND"));// 参观结束时间
					String OFFICEMANAGER = CoreUtil.objToStr(bodydata.get("OFFICEMANAGER"));// 办公室人员id
					if (OFFICEMANAGER != null || !"".equals(OFFICEMANAGER)) {
						String[] people = OFFICEMANAGER.split(" ");
						for (int j = 0; j < people.length; j++) {
							String mobile = CoreUtil.objToStr(DBSql.getString(
									"select mobile from ORGUSER  " + " where  userid = '" + people[j] + "'", "mobile"));// 参观接待人电话号
							System.out.println("mobile is" + mobile);
							String timenum =VISITDATE + " " + VISITDATETIME + "-" + VISITDATETIMEEND+"，共"+visitornum+"人";
//							String timenum =VISITDATE + " " + VISITDATETIME;
							String param = "{'APPLYMESSAGE':'" + targetunit + targetdept + "','APPLYNAME':'" + targetman
									+ "','LOCATIONNAME':'" + LOCATIONNAME + "','VISITDATE':'" + timenum 
									+ "','TARGETMANPHONE':'" + targetmanphone
									+ "','ENTOURAGE':'" + ptryzzandname
									+ "','CARNOS':'" + num.toString() + "'}";// 总的车牌信息
							if ("院部".equals(TARGETUNIT) || "805所".equals(TARGETUNIT)) {
								if (!"".equals(mobile) && inout.length() > 0) {
									String param1 = "{'APPLYMESSAGE':'" + targetunit + targetdept + "','APPLYNAME':'"
											+ targetman + "','LOCATIONNAME':'" + LOCATIONNAME+"','VISITDATE':'"+ timenum
											+ "','TARGETMANPHONE':'" + targetmanphone 
											+ "','ENTOURAGE':'" + ptryzzandname + "','CARNOS':'"
											+ inout.toString().substring(0, inout.length() - 1) + "'}";// 总的进入园区车辆信息和园区门
									System.out.println("param1 is" + param1);
									JSONObject result=SmsUtil.sendSms(mobile, "SMS_462610929", param1);// 接待人发短信提醒
									System.out.println("result is" + result);
								} else if (!"".equals(mobile)) {
									System.out.println("param is" + param);
									JSONObject result=SmsUtil.sendSms(mobile, "SMS_462610929", param);// 接待人发短信提醒
									System.out.println("result is" + result);
								}

							} else {
								if (!"".equals(mobile)) {
									System.out.println("param is" + param);
									JSONObject result=SmsUtil.sendSms(mobile, "SMS_462610929", param);// 接待人发短信提醒
									System.out.println("result is" + result);
								}
							}
						}
					}
				}
			}

			if ("".equals(num.toString()) && (bodyList != null || bodyList.size() > 0)) {
				for (int i = 0; i < bodyList.size(); i++) {
					Map<String, Object> bodydata = bodyList.get(i);
					String LOCATIONNAME = CoreUtil.objToStr(bodydata.get("LOCATIONNAME"));// 地点名称
					String VISITDATE = CoreUtil.objToStr(bodydata.get("VISITDATE"));// 预计参观日期
					String VISITDATETIME = CoreUtil.objToStr(bodydata.get("VISITDATETIME"));// 参观开始时间
					String VISITDATETIMEEND = CoreUtil.objToStr(bodydata.get("VISITDATETIMEEND"));// 参观结束时间
					String OFFICEMANAGER = CoreUtil.objToStr(bodydata.get("OFFICEMANAGER"));// 办公室人员id
					if (OFFICEMANAGER != null || !"".equals(OFFICEMANAGER)) {
						String[] people = OFFICEMANAGER.split(" ");
						for (int j = 0; j < people.length; j++) {
							String mobile = CoreUtil.objToStr(DBSql.getString(
									"select mobile from ORGUSER  " + " where  userid = '" + people[j] + "'", "mobile"));// 参观接待人电话号
							System.out.println("mobile is" + mobile);
							String timenum =VISITDATE + " " + VISITDATETIME + "-" + VISITDATETIMEEND+"，共"+visitornum+"人";
//							String timenum =VISITDATE + " " + VISITDATETIME;
							// 发消息
							// 参观申请：808所XXX部门的张XX处长申请参观：运载火箭远程支持中心，预计参观时间：08-02 10:00-11:00，申请人电话：XXXXX。
							String msg = "参观申请：" + targetunit + targetdept + "的" + targetman + "申请参观：" + LOCATIONNAME
									+ "，预计参观时间：" + VISITDATE + " " + VISITDATETIME + "-" + VISITDATETIMEEND + "，申请人电话："
									+ targetmanphone + "。";
							String param = "{'APPLYMESSAGE':'" + targetunit + targetdept + "','APPLYNAME':'" + targetman
									+ "','LOCATIONNAME':'" + LOCATIONNAME+"','VISITDATE':'" + timenum 
									+ "','TARGETMANPHONE':'" + targetmanphone
									+ "','ENTOURAGE':'" + ptryzzandname
									+ "'}";
							if (!"".equals(mobile)) {
								System.out.println("param is" + param);
								JSONObject result=SmsUtil.sendSms(mobile, "SMS_462635874", param);// 接待人发短信提醒 (不含车辆信息)
								System.out.println("result is" + result);
							}
						}
					}
				}
			}

		}
	}

}
